/**
 * @Author: yzy
 * @Description:
 * @Version: 1.0.0
 * @Date: 2021/7/9 15:57
 * @Copyright: MIN-Group；国家重大科技基础设施——未来网络北大实验室；深圳市信息论与未来网络重点实验室
 */
package analysisinterceptor

import (
	"context"
	"encoding/json"
	"github.com/go-playground/validator"
	"vpn-management-server/logger"
	"vpn-management-server/network"
	"vpn-management-server/security/code"
	"vpn-management-server/src/model/adminmodel"
	"vpn-management-server/src/model/analysismodel"
	"vpn-management-server/src/service/adminservice"
)

func GetUGroupDataAnalysisValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	var GetUGroupDataAnalysis analysismodel.GetUGroupDataAnalysis
	err := json.Unmarshal(request.Parameters, &GetUGroupDataAnalysis)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "UnMarshal Parameters to GetUGroupDataAnalysis error,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "parse parameters fail"
		return false
	}
	valid := validator.New()
	err = valid.Struct(GetUGroupDataAnalysis)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "validate Parameters fail,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "validate parameters fail"
		return false
	}
	// 审计管理员 超级管理员 用户组管理员
	if rank := adminservice.GetAdminRankService(ctx, request, response); rank == adminmodel.UserGroupAdmin {
		request.SetModel(&GetUGroupDataAnalysis)
		return true
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "have no authority")
		response.Code = code.UNAUTHORIZED
		response.ErrorMsg = "have no authority"
		return false
	}
}

func GetDataAnalysisValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	var GetDataAnalysis analysismodel.GetDataAnalysis
	err := json.Unmarshal(request.Parameters, &GetDataAnalysis)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "UnMarshal Parameters to GetUGroupDataAnalysis error,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "parse parameters fail"
		return false
	}
	valid := validator.New()
	err = valid.Struct(GetDataAnalysis)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "validate Parameters fail,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "validate parameters fail"
		return false
	}
	// 审计管理员 超级管理员 用户组管理员
	if rank := adminservice.GetAdminRankService(ctx, request, response); rank == adminmodel.SuperAdmin || rank == adminmodel.SystemAdmin || rank == adminmodel.AuditAdmin || rank == adminmodel.RuleAdmin {
		request.SetModel(&GetDataAnalysis)
		return true
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "have no authority")
		response.Code = code.UNAUTHORIZED
		response.ErrorMsg = "have no authority"
		return false
	}
}

func GetAnalysisInfosValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	// 只有超级管理员拥有查看的权限
	if rank := adminservice.GetAdminRankService(ctx, request, response); rank == adminmodel.SuperAdmin {
		return true
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "have no authority")
		response.Code = code.UNAUTHORIZED
		response.ErrorMsg = "have no authority"
		return false
	}
}

func GetLogsAnalysisValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	var logType analysismodel.LogType

	err := json.Unmarshal(request.Parameters, &logType)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "UnMarshal Parameters to UGroupID error,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "parse parameters fail"
		return false
	}

	valid := validator.New()
	err = valid.Struct(logType)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "validate Parameters fail,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "validate parameters fail"
		return false
	}

	// 只有超级管理员拥有查看的权限
	if rank := adminservice.GetAdminRankService(ctx, request, response); rank == adminmodel.SuperAdmin {
		request.SetModel(&logType)
		return true
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "have no authority")
		response.Code = code.UNAUTHORIZED
		response.ErrorMsg = "have no authority"
		return false
	}
}

func GetUGroupLogsAnalysisValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	var ugroupLogType analysismodel.UGroupLogType
	err := json.Unmarshal(request.Parameters, &ugroupLogType)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "UnMarshal Parameters to UGroupID error,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "parse parameters fail"
		return false
	}

	valid := validator.New()
	err = valid.Struct(ugroupLogType)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "validate Parameters fail,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "validate parameters fail"
		return false
	}

	if rank := adminservice.GetAdminRankService(ctx, request, response); rank == adminmodel.UserGroupAdmin {
		request.SetModel(&ugroupLogType)
		return true
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "have no authority")
		response.Code = code.UNAUTHORIZED
		response.ErrorMsg = "have no authority"
		return false
	}
}
